/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.property.subinfo.web;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.property.subinfo.entity.PropertyCarinfo;
import com.jeeplus.modules.property.subinfo.service.PropertyCarinfoService;
import com.jeeplus.modules.property.vilageinfo.entity.PropertyVillageinfo;
import com.jeeplus.modules.property.vilageinfo.service.PropertyVillageinfoService;
import com.jeeplus.modules.sys.entity.DictValue;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 车位信息Controller
 * @author 陈龙
 * @version 2019-01-09
 */
@Controller
@RequestMapping(value = "${adminPath}/property/carinfo/propertyCarinfo")
public class PropertyCarinfoController extends BaseController {

	@Autowired
	private PropertyCarinfoService propertyCarinfoService;
	@Autowired
	private PropertyVillageinfoService propertyVillageinfoService;
	@ModelAttribute
	public PropertyCarinfo get(@RequestParam(required=false) String id) {
		PropertyCarinfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = propertyCarinfoService.get(id);
		}
		if (entity == null){
			entity = new PropertyCarinfo();
		}
		return entity;
	}
	
	/**
	 * 车位信息列表页面
	 */
	@RequiresPermissions("property:carinfo:propertyCarinfo:list")
	@RequestMapping(value = {"list/{mode}", "/{mode}"})
	public String list(@PathVariable String mode,PropertyCarinfo propertyCarinfo, Model model) {
		model.addAttribute("propertyCarinfo", propertyCarinfo);
		List<PropertyVillageinfo> villList=propertyVillageinfoService.findList(new PropertyVillageinfo());
		List<DictValue> dvlist=new ArrayList<DictValue>();
		for(PropertyVillageinfo vinfo:villList){
			DictValue dv=new DictValue();
			dv.setLabel(vinfo.getVillname());
			dv.setValue(vinfo.getId());
			dvlist.add(dv);
		}
		model.addAttribute("dvlist", dvlist);
		if(mode!=null&&mode.equals("index")){
			return "modules/property/carinfo/propertyCarinfoListIndex";
		}
		return "modules/property/carinfo/propertyCarinfoList";
	}
	
		/**
	 * 车位信息列表数据
	 */
	@ResponseBody
	@RequiresPermissions("property:carinfo:propertyCarinfo:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(PropertyCarinfo propertyCarinfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PropertyCarinfo> page = propertyCarinfoService.findPage(new Page<PropertyCarinfo>(request, response), propertyCarinfo);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑车位信息表单页面
	 */
	@RequiresPermissions(value={"property:carinfo:propertyCarinfo:view","property:carinfo:propertyCarinfo:add","property:carinfo:propertyCarinfo:edit"},logical=Logical.OR)
	@RequestMapping(value = "form/{mode}")
	public String form(@PathVariable String mode, PropertyCarinfo propertyCarinfo, Model model) {
		model.addAttribute("propertyCarinfo", propertyCarinfo);
		model.addAttribute("mode", mode);
		return "modules/property/carinfo/propertyCarinfoForm";
	}

	/**
	 * 保存车位信息
	 */
	@ResponseBody
	@RequiresPermissions(value={"property:carinfo:propertyCarinfo:add","property:carinfo:propertyCarinfo:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public AjaxJson save(PropertyCarinfo propertyCarinfo, Model model) throws Exception{
		AjaxJson j = new AjaxJson();
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(propertyCarinfo);
		if (StringUtils.isNotBlank(errMsg)){
			j.setSuccess(false);
			j.setMsg(errMsg);
			return j;
		}
		//新增或编辑表单保存
		propertyCarinfoService.save(propertyCarinfo);//保存
		j.setSuccess(true);
		j.setMsg("保存车位信息成功");
		return j;
	}
	
	/**
	 * 删除车位信息
	 */
	@ResponseBody
	@RequiresPermissions("property:carinfo:propertyCarinfo:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(PropertyCarinfo propertyCarinfo) {
		AjaxJson j = new AjaxJson();
		propertyCarinfoService.delete(propertyCarinfo);
		j.setMsg("删除车位信息成功");
		return j;
	}
	
	/**
	 * 批量删除车位信息
	 */
	@ResponseBody
	@RequiresPermissions("property:carinfo:propertyCarinfo:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			propertyCarinfoService.delete(propertyCarinfoService.get(id));
		}
		j.setMsg("删除车位信息成功");
		return j;
	}
	
	/**
	 * 导出excel文件
	 */
	@ResponseBody
	@RequiresPermissions("property:carinfo:propertyCarinfo:export")
    @RequestMapping(value = "export")
    public AjaxJson exportFile(PropertyCarinfo propertyCarinfo, HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		try {
            String fileName = "车位信息"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<PropertyCarinfo> page = propertyCarinfoService.findPage(new Page<PropertyCarinfo>(request, response, -1), propertyCarinfo);
    		new ExportExcel("车位信息", PropertyCarinfo.class).setDataList(page.getList()).write(response, fileName).dispose();
    		j.setSuccess(true);
    		j.setMsg("导出成功！");
    		return j;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导出车位信息记录失败！失败信息："+e.getMessage());
		}
			return j;
    }

	/**
	 * 导入Excel数据

	 */
	@ResponseBody
	@RequiresPermissions("property:carinfo:propertyCarinfo:import")
    @RequestMapping(value = "import")
   	public AjaxJson importFile(@RequestParam("file")MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<PropertyCarinfo> list = ei.getDataList(PropertyCarinfo.class);
			for (PropertyCarinfo propertyCarinfo : list){
				try{
					propertyCarinfoService.save(propertyCarinfo);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条车位信息记录。");
			}
			j.setMsg( "已成功导入 "+successNum+" 条车位信息记录"+failureMsg);
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导入车位信息失败！失败信息："+e.getMessage());
		}
		return j;
    }
	
	/**
	 * 下载导入车位信息数据模板
	 */
	@ResponseBody
	@RequiresPermissions("property:carinfo:propertyCarinfo:import")
    @RequestMapping(value = "import/template")
     public AjaxJson importFileTemplate(HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		try {
            String fileName = "车位信息数据导入模板.xlsx";
    		List<PropertyCarinfo> list = Lists.newArrayList();
    		new ExportExcel("车位信息数据", PropertyCarinfo.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg( "导入模板下载失败！失败信息："+e.getMessage());
		}
		return j;
    }

}